=begin
  1. 点评记录 - review
  2. 收藏记录 - favorite
  3. 分享记录 - share_item
  4. 推荐商品记录 - recommend_item
  5. 推荐点评记录 - recommend_review
  6. 好友记录 - friend

  DEMO:
  require 'operation_records/operation_record'
  OperationRecord.create_operation_line(OperationRecord::FLAG.review, 21, 111, "test")

=end

#require 'erubis'
require 'erb'
require 'ostruct'
require 'ruby-debug'

module OperationRecord
  
  TYPE_FLAG_MAP = {
                :review           => 1, 
                :favorite         => 2,
                :share_item       => 3, 
                :recommend_item   => 4,
                :recommend_review => 5,
                :friend           => 6
              }
              
  FLAG_TYPE_MAP = TYPE_FLAG_MAP.invert
  
  FLAG = OpenStruct.new(TYPE_FLAG_MAP)

  #添加一条用户操作记录                        
  def self.create_operation_line(flag, user_id, item_id=nil, item_name=nil, friend_id=nil, friend_name=nil)
    config = load_operation_template
    content = ERB.new(config[FLAG_TYPE_MAP[flag].to_s])
    UserOperationRecord.create(:user_id => user_id, :content => content.result(binding), :flag => flag)
  end                       
                
  private
  
  def self.load_operation_template
  	YAML.load(File.read("#{RAILS_ROOT}/lib/operation_records/template.yml"))
  end
end